package com.hzw.saas.service.product.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzw.saas.api.product.IProductService;
import com.hzw.saas.api.product.dto.ProductDetailsDto;
import com.hzw.saas.api.product.dto.ProductDto;
import com.hzw.saas.api.product.dto.ProductResourceDto;
import com.hzw.saas.common.config.util.PageParam;
import com.hzw.saas.common.config.util.PageUtils;
import com.hzw.saas.common.util.MapperUtil;
import com.hzw.saas.service.product.mapper.ProductMapper;
import com.hzw.saas.service.product.mapper.ProductResourceMapper;
import com.hzw.saas.service.product.model.Product;
import com.hzw.saas.service.product.model.ProductResource;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Service;
import lombok.AllArgsConstructor;
import ma.glasnost.orika.MapperFacade;

/**
 * <p>
 * 商品 服务实现类
 * </p>
 *
 * @author sonam
 * @since 2021-01-19
 */
@Service("productServiceImpl")
@AllArgsConstructor
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService, IService<Product> {

    private final ProductResourceMapper productResourceMapper;

    private final MapperFacade mapperFacade;

    @Override
    public String getProductName(String productId) {
        Product product = this.getOne(Wrappers.<Product>lambdaQuery()
            .select(Product::getProdName)
            .eq(Product::getProdId, productId));
        if (product != null)
            return product.getProdName();
        return null;
    }

    @Override
    public IPage<ProductDto> searchByCouldTypeAndSubTypePage(PageParam pageParam, Integer cloudType, Integer subType) {
        Page<Product> productPage = this.page(pageParam.convert(), Wrappers.<Product>lambdaQuery()
            .eq(Product::getCloudType, cloudType)
            .eq(Objects.nonNull(subType), Product::getSubType, subType)
            .orderByAsc(Product::getSeq));
        return PageUtils.convert(productPage, ProductDto.class);
    }

    @Override
    public IPage<ProductDto> getProductsByCloudTypes(PageParam page, Integer... cloudTypes) {
        List<Integer> cloudTypeList = Objects.isNull(cloudTypes) ? new ArrayList<>() : Arrays.asList(cloudTypes);
        IPage<Product> list = this.page(page.convert(), Wrappers.<Product>lambdaQuery()
            .in(!cloudTypeList.isEmpty(), Product::getCloudType, cloudTypeList)
            .orderByAsc(Product::getSeq));
        IPage<ProductDto> dtoList = PageUtils.convert(list, ProductDto.class);
        dtoList.getRecords().forEach(ProductDto::onReady);
        return dtoList;
    }

    @Override
    public ProductDto getProductById(String productId) {
        if (StrUtil.isBlank(productId))
            return null;
        Product product = super.getById(productId);
        return MapperUtil.nf().map(product, ProductDto.class);
    }

    @Override
    public List<ProductDto> listProducts(List<String> productIds) {
        if (CollectionUtils.isEmpty(productIds))
            return new ArrayList<>();
        List<Product> products = super.listByIds(productIds);
        return MapperUtil.nf().mapAsList(products, ProductDto.class);
    }

    @Override
    public ProductDetailsDto getProductDetailById(String productId) {
        Product product = getById(productId);
        ProductDetailsDto productDetailsDto = mapperFacade.map(product, ProductDetailsDto.class);

        List<ProductResource> productResourceList = productResourceMapper.selectList(Wrappers.<ProductResource>lambdaQuery()
            .eq(ProductResource::getProdId, productId));
        if (CollectionUtils.isNotEmpty(productResourceList)) {
            productDetailsDto.setProductResourceList(mapperFacade.mapAsList(productResourceList, ProductResourceDto.class));
        }

        return productDetailsDto;
    }
}
